package com.epam.command.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.epam.command.logic.BuyLogic;
import com.epam.command.logic.Command;
import com.epam.dao.exception.TourNotAvailableException;
import com.epam.util.ConfigurationManager;

public class BuyCommand implements Command {

	private static Logger LOG = Logger.getLogger(BuyCommand.class);
	
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) {
		
		HttpSession session = request.getSession();
		String page;
		
		int id = (Integer) session.getAttribute("user_id");
		int voucher = Integer.parseInt(request.getParameter("voucher")); 
		
			try {
				BuyLogic.buy(id, voucher);
				page = ConfigurationManager.getConfig(ConfigurationManager.OPERATION_SUCCESSFUL);
			} catch (TourNotAvailableException e) {
				page = ConfigurationManager.getConfig(ConfigurationManager.PURCHASE_ERROR);
				LOG.error(e);
			}
			
		
		return page;
	}

}
